package com.example.moilapp;

import com.example.moilapp.service.Moil_Login_Service;
import com.example.moilapp.service.Moil_Status_Service;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class Moil_Login  extends Activity{

	public static final String PREFS_NAME = "LoginDetailsFile";
	
	public EditText username;
	public EditText password;
	public CheckBox remember;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.moil_login);
		
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	    String moil_user = settings.getString("UserName", "");
	    String moil_password = settings.getString("Password", "");
		
	    username = (EditText)findViewById(R.id.username);
	    password = (EditText)findViewById(R.id.password);
	    
	    
	    
	    remember = (CheckBox)findViewById(R.id.rememberpass_check);
	    
	    Button btnlogin = (Button) findViewById(R.id.Loginbutton);
	    btnlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(remember.isChecked()){
				      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
				      SharedPreferences.Editor editor = settings.edit();
				      editor.putString("UserName", username.getText().toString());
				      editor.putString("Password", password.getText().toString());
				      
				      editor.commit();
			    }
				
				Moil_Login_Service mps = new Moil_Login_Service(Moil_Login.this,username.getText().toString(),password.getText().toString());
				mps.execute(new String[] {"http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx"});
				
				
			}
		});
	    
	    username.setText(moil_user);
	    password.setText(moil_password);
	    
	    Button btnlogoout = (Button) findViewById(R.id.btn_close);
		btnlogoout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
	    
	}
	
	@Override
    protected void onStop(){
      super.onStop();

      if(remember.isChecked()){
	      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	      SharedPreferences.Editor editor = settings.edit();
	      editor.putString("UserName", username.getText().toString());
	      editor.putString("Password", password.getText().toString());
	      
	      editor.commit();
      }
    }
	
}
